// pages/member/memberaddress.js
const app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    address_list:{
      length:0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    app.sendRequest({
      url: 'api.php?s=member/getmemberaddresslist',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          let address_list = res.data.data;
          that.setData({
            address_list: address_list
          });
          console.log(res);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 选择收货地址
   */
  selectAddress: function (event) {
    let that = this;

    let key = event.currentTarget.dataset.key;
    let address_id = event.currentTarget.dataset.id;
    let address_list = that.data.address_list;

    app.sendRequest({
      url: 'api.php?s=member/updateAddressDefault',
      data: {
        id: address_id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          if (res.data > 0){
            for (let i=0; i<address_list.length ; i++){
              address_list[i].is_default = 0;
            }
            address_list[key].is_default = 1;
          }
          that.setData({
            address_list: address_list
          });
        }
      }
    })
  },

  /**
   * URL跳转
   */
  aClick:function (event) {
    let url = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages'+url,
    })
  },
  
  /**
   * 删除地址
   */
  delAddress: function(event){
    let that = this;

    let key = event.currentTarget.dataset.key;
    let address_id = event.currentTarget.dataset.id;
    let is_default = event.currentTarget.dataset.default;
    let address_list = that.data.address_list;
    
    if (is_default == 1){
      app.showBox(that, '默认地址不能删除');
      return false;
    }

    app.sendRequest({
      url: 'api.php?s=member/memberAddressDelete',
      data: {
        id: address_id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          if (res.data > 0) {
            address_list.splice(key,1);
            app.showBox(that, '删除成功');
            that.setData({
              address_list: address_list
            });
          }
          if (res.data == -2007){
            app.showBox(that, '当前用户默认地址不能删除');
          }
        }
      }
    })
  }
})